package com.example.yuekao;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.yuekao.fragmenter.FourFragment;
import com.example.yuekao.fragmenter.OneFragment;
import com.example.yuekao.fragmenter.ThreeFragment;
import com.example.yuekao.fragmenter.TwoFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ViewPager mVp;
    private TabLayout mTl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }

    private void initViews() {
        mVp = findViewById(R.id.vp);
        mTl = findViewById(R.id.tl);

        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new OneFragment());
        fragments.add(new TwoFragment());
        fragments.add(new ThreeFragment());
        fragments.add(new FourFragment());

        mVp.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });
        mTl.setupWithViewPager(mVp);

        mTl.getTabAt(0).setText("首页");
        mTl.getTabAt(1).setText("消息");
        mTl.getTabAt(2).setText("咨询");
        mTl.getTabAt(3).setText("我");
    }
}